# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.


from odoo import api, http, SUPERUSER_ID, _
from odoo.http import request

from odoo import registry as registry_get

import base64


class Upload(http.Controller):

    @http.route('/fapiao/upload', type='http', auth='user')
    def upload(self, *args, **kw):
        if request.httprequest.method == 'POST':
            ufile = kw.get('file')

            if not ufile:
                return {'error': 'no file'}

            try:
                # Model = request.env['ir.attachment']
                # filename = ufile.filename
                # attachment = Model.search(
                #     [('name', '=', filename)], limit=1)

                # if not attachment:
                #     attachment = Model.create({
                #         'name': filename,
                #         'datas': base64.encodebytes(ufile.read()),
                #         # 'res_model': model,
                #         # 'res_id': int(id)
                #     })

                # print(attachment)

                #
                # content_type = ufile.content_type
                # s = ufile.read()

                # print(s)
                # print(filename)
                # print(content_type)
                response = request.render('vwat_fapiao.upload_ok')
                return response

            except Exception:
                return {'error': 'no file'}

        else:

            response = request.render('vwat_fapiao.upload')
            return response
